﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using Moop.Common;

namespace Moop.UI.UIServices
{
    public class UIService
    {
        public TreeView GetCubeTreeView(ObservableCollection<ICube> Cubes, string SelectedCubeName)
        {
            TreeView mainTreeView = new TreeView();

            foreach (ICubeDimension dim in Cubes.Where(p => p.Name == SelectedCubeName).FirstOrDefault().CubeDimensions.Values.OrderBy(p => p.Name))
            {
                var folders = dim.Attributes.Where(p => (!string.IsNullOrEmpty(p.DisplayFolder))).GroupBy(p => p.DisplayFolder);

                TreeViewItem dimTreeItem = new TreeViewItem() { Header = dim.Name, ToolTip = dim.OlapPath };

                #region Sets

                if (dim.Sets.Count > 0)
                {
                    TreeViewItem setsTreeItem = new TreeViewItem() { Header = "Sets" };
                    foreach (ICubeDimensionSet set in dim.Sets)
                    {
                        setsTreeItem.Items.Add(new TreeViewItem() { Header = set.Name, ToolTip = set.OlapPath });
                    }
                    dimTreeItem.Items.Add(setsTreeItem);
                }

                #endregion


                #region Attrs

                if (dim.Attributes.Count > 0)
                {
                    TreeViewItem attrTreeItem = new TreeViewItem() { Header = "Attributes" };
                    var displayFolders = dim.Attributes.Where(p => !string.IsNullOrEmpty(p.DisplayFolder)).DistinctBy(p => p.DisplayFolder).Select(p => p.DisplayFolder);

                    //Attr. In Folder
                    foreach (string folder in displayFolders)
                    {
                        TreeViewItem folderTreeViewItem = new TreeViewItem() { Header = folder };
                        foreach (ICubeDimensionAttribute attr in dim.Attributes.Where(p => p.DisplayFolder == folder))
                        {
                            folderTreeViewItem.Items.Add(new TreeViewItem() { Header = attr.Name, ToolTip = attr.OlapPath });
                        }
                        dimTreeItem.Items.Add(folderTreeViewItem);
                    }

                    //Attrs Not In Folder
                    foreach (ICubeDimensionAttribute attr in dim.Attributes.Where(p => string.IsNullOrEmpty(p.DisplayFolder)))
                    {
                        #region Add Menu Item

                        var tvItem = new TreeViewItem() { Header = attr.Name, ToolTip = attr.OlapPath };
                        var ctxMenu = new ContextMenu();
                        ctxMenu.Items.Add(new TextBlock() { Text = "Fetch 10 More members..." });
                        tvItem.ContextMenu = ctxMenu; 

                        #endregion

                        attrTreeItem.Items.Add(new TreeViewItem() { Header = attr.Name, ToolTip = attr.OlapPath });
                    }                    

                    dimTreeItem.Items.Add(attrTreeItem);
                }

                #endregion


                #region Heirarchies

                if (dim.Hierarchies.Count > 0)
                {
                    TreeViewItem heirTreeItem = new TreeViewItem() { Header = "Hierarchies" };
                    foreach (ICubeDimensionHierarchy heir in dim.Hierarchies)
                    {
                        heirTreeItem.Items.Add(new TreeViewItem() { Header = heir.Name, ToolTip = heir.OlapPath });

                        //TODO: Add another foreach for Heirarchy Levels
                        foreach (ICubeDimensionHierarchyLevel heirLevel in heir.Levels)
                        {
                           // heirTreeItem.Items.Add(new TreeViewItem() { Header = heir.Name, ToolTip = heir.OlapPath });
                        }
                    }
                    dimTreeItem.Items.Add(heirTreeItem);
                }

                #endregion

                mainTreeView.Items.Add(dimTreeItem);
            }

            return mainTreeView;
        }

    }
}
